<?php
class Impact_FileUpload
{
    const NO_FILE_UPLOADED  = 'noFileUploaded';
    const ERROR             = 'error';
    const SUCCESSFUL        = 'successful';

    protected $_phpCode = null;
    protected $_status;
    protected $_size;
    protected $_tmpName;
    protected $_mimeType;
    protected $_fileName;

    public function __construct($filesArrayKey)
    {
        if (!isset($_FILES[$filesArrayKey])) {
            $this->_status = self::NO_FILE_UPLOADED;
        } else {
            $this->_processFileData($_FILES[$filesArrayKey]);
        }
    }

    public function move($toPath)
    {
        return move_uploaded_file($this->_tmpName, $toPath);
    }

    public function getErrorCode()
    {
        return $this->_phpCode;
    }

    public function getStatus()
    {
        return $this->_status;
    }

    public function getSize()
    {
        return $this->_size;
    }

    public function getTempName()
    {
        return $this->_tmpName;
    }

    public function getMimeType()
    {
        return $this->_mimeType;
    }

    public function getFileName()
    {
        return $this->_fileName;
    }

    protected function _processFileData($fileData)
    {
        $this->_phpCode = $fileData['error'];

        if ($this->_phpCode == UPLOAD_ERR_NO_FILE) {
            $this->_status = self::NO_FILE_UPLOADED;
        } else if ($this->_phpCode != 0) {
            $this->_status = self::ERROR;
        } else if ($fileData['size'] == 0) {
            $this->_status = self::NO_FILE_UPLOADED;
        } else {
            $this->_status = self::SUCCESSFUL;
        }

        if ($this->_status == self::SUCCESSFUL) {
            $this->_size = $fileData['size'];
            $this->_tmpName = $fileData['tmp_name'];
            $this->_mimeType = $fileData['type'];
            $this->_fileName = $fileData['name'];
        }
    }
}
// vim: sw=4:ts=4:sts=4:et
